﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using Baosight.ZNH.ZMCommon;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI;
using Microsoft.Practices.Unity;
using Baosight.ZNH.ZMCB.UI.PopWindows;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCB.UI.Utils;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls.GridView;


namespace Baosight.ZNH.ZMCB.UI
{
    /// <summary>
    /// 采面信息管理
    /// </summary>
    /// Title:ZMCB04,ServiceZMCB04
    /// Copyright:Baosight Software LTD.co Copyright (c) 2013
    /// Company:上海宝信软件股份有限公司
    /// Author: 李向武
    /// Version: 1.0
    /// History:
    ///       2013-6-18  李向武   [创建]
    ///
    public partial class ZMCB04 : EFViewCmes
    {
        private WorkingFaceBlock _block = new WorkingFaceBlock();//私有化下拉列表
        private string ButtonName = "";
        public ZMCB04()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }
        
        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 点击新增按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            ButtonName = "insert";
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "采面信息管理-新增", "insert", null, sender, e);

        }

        /// <summary>
        /// 点击修改按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            ButtonName = "update";
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            base.OnUpdateButtonClickPopup(eiInfo, "采面信息管理-修改", "update", null, sender, e);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            ButtonName = "delete";
            base.OnDeleteButtonClick(sender, e);
        }

        /// <summary>
        /// 重写基类方法：根据数据的改变更新主页面的数据
        /// </summary>
        /// <param name="data"></param>
        public override void UpdateHomePageData(EIInfo data)
        {
            // 数据发布
            IEventAggregator eventAggreator = Container.Resolve<IEventAggregator>();
            EIDataPublishedEvent dataPublishedEvent = eventAggreator.GetEvent<EIDataPublishedEvent>();
            dataPublishedEvent.Publish(data);
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                this.grid.Refresh();//刷新Grid区域
                this.UpdateHomePageData(new EIInfo());//发布数据
            }

            //按钮生效
            base.TempButtonOpen(true);
            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            WorkingFace _works = new WorkingFace(_dialogType);//构造一个新的修改页面
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.recoveryProcess"));//回采工艺
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.coalType"));//采煤方法
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cb.isFirst"));//是否采区首采
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cb.status"));//施工状态
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cb.parentInforWf"));//采面所属下拉框
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cb.parentInforC"));//所属煤层下拉框
            _works.EIDataContext = new Platform.Core.UI.EIDataContext();
            if (this.fdParentId.Value != null)
            {
                if (!this.fdParentId.Value.ToString().Equals(""))
                {
                    _works.fdParentId.IsEnabled = false;
                    _works.fdParentId.Value = this.fdParentId.Value;
                }
            }
          

            if (_dialogType.Equals("update") || _dialogType.Equals("detail"))
            {
                ((_works.fdMain.Content) as CmesCustomView).Value = _eIInfo.GetBlock("result").GetRow(0)["fdMainId"];
                ((_works.fdMain.Content) as CmesCustomView).Display = _eIInfo.GetBlock("result").GetRow(0)["fdMain"];
                ((_works.fdMain.Content) as CmesCustomView).eViewInitParams = "bindingService=ZMCB08.query,initParam=fdMainId:" + _eIInfo.GetBlock("result").GetRow(0)["fdKeyId"];
            }
            else
            {
                ((_works.fdMain.Content) as CmesCustomView).eViewInitParams = "bindingService=ZMCB08.query,initParam=fdMainId:null";
            }
            
            _works.EIDataContext.Update(_eIInfo);
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = _works;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }
        /// <summary>
        /// 点击查看按钮执行的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;
            EIRow row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow());
            //EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "采面信息管理-查看", "detail", null, sender, e);
        }

        /// <summary>
        /// 删除的判断
        /// </summary>
        /// <param name="rowList"></param>
        public override bool BeforeMethod(List<EIRow> rowList)
        {
            bool DeleteFlag = true;
            if (ButtonName.Equals("delete"))
            {
                foreach (var row in rowList)
                {
                    if (row["fdStatus"].Equals("开工"))
                    {
                        base.OpenEFWindowAlert("勾选的记录包含开工记录，不能删除，请重新选择需要删除的记录!", true);
                        DeleteFlag = false;
                        break;
                    }
                }
            }
           
            return DeleteFlag;
        }
    }
}
